package com.rjwh.dingdong.client.view;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.UrlUtil;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.view.View.GONE;

/**
 * @author Kay
 * @ClassName DrawBookStepDailog
 * @Description
 * @date 2016-8-10
 * @history 2016-8-10 author: description:
 */
public class DrawBookStepDailog extends AlertDialog {

    @BindView(R.id.draw_book_step_dailog_webview)
    BaseWebView webView;
    private OnCancelClickListener onCancelClickListener;
private String mUrl;

    public DrawBookStepDailog(Context context, OnCancelClickListener onCancelClickListener,String url) {
        super(context, R.style.defaultDialog);
        this.onCancelClickListener = onCancelClickListener;
        mUrl = url;
        LogUtil.d("DrawBookStepDailog", "new DrawBookStepDailog");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.d("DrawBookStepDailog", "onCreate");
        setContentView(R.layout.draw_book_step_dailog_layout);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setGravity(Gravity.CENTER);
        this.setCancelable(false);
        ButterKnife.bind(this);
        setWebView();
    }

    public void setWebView() {
        LogUtil.d("DrawBookStepDailog", "setWebView");
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");  //设置默认的显示编码
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webView.setBackgroundColor(0); // 设置背景透明
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return (event.getAction() == MotionEvent.ACTION_MOVE);
            }
        });
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 处理自定义scheme
                if (url.contains("tiandilixin")) {
                    LogUtil.d("DrawBookStepDailog", "handelUrl" + url);
                    UrlUtil.handelUrl(getContext(), url, true);
                    onCancelClickListener.onCancel();
                } else {
                    view.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                LogUtil.e("DrawBookStepDailog", "onPageFinished: " + url);
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                LogUtil.e("DrawBookStepDailog", errorCode + " ;" + description);
                view.setVisibility(GONE);
            }

        });
        webView.loadUrl(mUrl);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (webView != null) {
            webView.destroy();
        }
    }

    @OnClick(R.id.draw_book_step_dailog_close_btn)
    public void onClick() {
        onCancelClickListener.onCancel();
    }

    public interface OnCancelClickListener {
        void onCancel();
    }
}
